#include "para.h"
#include "hem_common.h"
#include "flash.h"
#include "fmc_operation.h"
/**
 * @brief 存储配置参数
 * [param in] *p_config 参数结构体指针
 */
int32_t hem_config_save(hem_config_t *p_config) 
{
  hem_config_t config;

  uint32_t size = sizeof(hem_config_t);
  fmc_read_32bit_data((uint32_t)USER_DATA_ADDR_BASE, size / 4, (int32_t *)&config);

  if (!memcmp(p_config, &config, size)) { // not chang, return
    return 0;
  }

	Flash_erase_page((uint32_t)USER_DATA_ADDR_BASE);
	
  return fmc_write_32bit_data_with_sector_erased((uint32_t)USER_DATA_ADDR_BASE, size / 4, (const uint32_t *)p_config);
}

int32_t hem_config_load(hem_config_t *p_config) 
{
  uint32_t size = sizeof(hem_config_t);
  fmc_read_32bit_data((uint32_t)USER_DATA_ADDR_BASE, size / 4, (int32_t *)p_config);
  if (p_config->config_ok != FLASH_CONFIG_OK)
    return -1;

  return 0;
}


